import { server } from '../config/server';
import { useToken } from '../utils/token';

export const school = {
    // 获取所有院校信息
    getAllSchool() {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/school/getAllSchool`,
                method: 'GET',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                success: (res) => {
                    console.log('【 Response in request getAllSchool 】', res);
                    let provinceList = [];
                    let allSchool = res.data.data;
                    for (let i = 0; i < allSchool.length; i++) {
                        let school = allSchool[i];
                        let find = false;
                        for (let j = 0; j < provinceList.length; j++) {
                            if (school.pid == provinceList[j].id) {
                                provinceList[j].schoolList.push(school);
                                find = true;
                                break;
                            }
                        }
                        if (!find) {
                            provinceList.push({
                                id: school.pid,
                                name: school.pname,
                                schoolList: [school]
                            });
                        }
                    }
                    resolve({ provinceList });
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getAllSchool 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    // 获取某个院校信息
    getSchoolInfo(collegeId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/school/getSchoolInfo`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: {
                    collegeId
                },
                success: (res) => {
                    console.log('【 Response in request getSchoolInfo 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getSchoolInfo 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 收藏院校
     */
    collectSchool(collegeId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/school/collectSchool`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: {
                    collegeId
                },
                success: (res) => {
                    console.log('【 Response in request collectSchool 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request collectSchool 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 取消收藏院校
     */
    noCollectSchool(collegeId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/school/noCollectSchool`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: {
                    collegeId
                },
                success: (res) => {
                    console.log('【 Response in request noCollectSchool 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request noCollectSchool 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 获取院校所有专业信息
     */
    getAllMajor(collegeId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/school/getAllMajor`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: {
                    collegeId
                },
                success: (res) => {
                    console.log('【 Response in request getAllMajor 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getAllMajor 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 获取院校所有资讯信息
     */
    getAllzixun(collegeId, lastId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/school/getAllzixun`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: {
                    collegeId, lastId
                },
                success: (res) => {
                    console.log('【 Response in request getAllzixun 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getAllzixun 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 获取院校咨询内容
     */
    getzixunContent(zixunId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/school/getzixunContent`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: {
                    zixunId
                },
                success: (res) => {
                    console.log('【 Response in request getzixunContent 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getzixunContent 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 获取推荐资讯信息
     */
    getRecommendzixun(lastId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/school/getRecommendzixun`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: {
                    lastId
                },
                success: (res) => {
                    console.log('【 Response in request getRecommendzixun 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getRecommendzixun 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    getCollectSchool(lastId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/school/getCollectSchool`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { lastId },
                success: (res) => {
                    console.log('【 Response in request getCollectSchool 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getCollectSchool 】', err);
                    reject({ success: false, err });
                }
            });
        });
    }
};